package com.sky.financelake.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.facebook.shimmer.ShimmerFrameLayout;
import com.sky.financelake.R;
import com.sky.financelake.adapter.SimpleStockAdapter;
import com.sky.financelake.base.BaseToolA;
import com.sky.financelake.bean.dto.SimpleDayKDto;
import com.sky.financelake.databinding.ActivityThreeRedBottomBinding;
import com.sky.financelake.viewmodels.remote.QuotationViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class ThreeRedBottomA extends BaseToolA {

    private ActivityThreeRedBottomBinding binding;
    private SimpleStockAdapter adapter;
    private List<SimpleDayKDto> simpleDayKDtos = new ArrayList<>();
    private QuotationViewModel quotationViewModel;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityThreeRedBottomBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        View v = LayoutInflater.from(this).inflate(R.layout.loading_layout, null);

        quotationViewModel = new ViewModelProvider(this).get(QuotationViewModel.class);

        Toolbar toolbar = binding.toolbarContainer.toolbar;
        initToolbar(toolbar, getString(R.string.three_red_bottom));

        ShimmerFrameLayout shimmerFrameLayout = binding.loading.shimmerContainer;

        SwipeRefreshLayout swipeRefreshLayout = binding.swipeRefresh;
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shimmerFrameLayout.setVisibility(View.VISIBLE);
                simpleDayKDtos.clear();
                adapter.notifyDataSetChanged();
                quotationViewModel.getThreeRed();
                toolbar.setSubtitle("");
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        RecyclerView rvStock = binding.rvStock;
        rvStock.setLayoutManager(new GridLayoutManager(this, 4));
        adapter = new SimpleStockAdapter(simpleDayKDtos, this);
        rvStock.setAdapter(adapter);

        quotationViewModel.getThreeRedLiveData().observe(this, response -> {
            List<SimpleDayKDto> tmp = response.getResult();
            simpleDayKDtos.addAll(tmp);
            adapter.notifyDataSetChanged();
            toolbar.setSubtitle(tmp.size() + "条数据");
            shimmerFrameLayout.setVisibility(View.GONE);
        });

        quotationViewModel.getThreeRed();
    }

}
